#include "form_setting_language.h"
#include "ui_form_setting_language.h"

// add nacessary includes here
#include "utils/configure/configure_bll.h"


/*
 * Function:    FormSettingLanguage
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormSettingLanguage::FormSettingLanguage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormSettingLanguage)
{
    qDebug() << Q_FUNC_INFO;

    ui->setupUi(this);
}

/*
 * FormHistoryResult:    ~FormSettingLanguage
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormSettingLanguage::~FormSettingLanguage()
{
    qDebug() << Q_FUNC_INFO;

    delete ui;
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormSettingLanguage" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "FormSettingLanguage" class
 */
FormSettingLanguage* FormSettingLanguage::getInstance(QWidget *pParent)
{
    static FormSettingLanguage *pNew = NULL;
    if(!pNew) {
        qDebug() << Q_FUNC_INFO;

        // Create a new object if it doesn't exist
        pNew = new FormSettingLanguage(pParent);
        if(NULL == pNew) return NULL;

        // After constructing object, initialize the object
        pNew->_initialze();

        // Initialize control's UI
        pNew->_initUi();

        // Hide widget firstly
        pNew->hide();
    }
    else {
        // Set the parent object
        pNew->setParent(pParent);
    }

    return pNew;
}

/*
 * Function:    _initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormSettingLanguage::_initialze()
{
    qDebug() << Q_FUNC_INFO;

    // Signal-slot initialization
    std::vector<bool> vecConnect;

    vecConnect.push_back(QObject::connect(this, SIGNAL(signalRefreshUI()), this, SLOT(slotRefreshUI()), static_cast<Qt::ConnectionType>(Qt::AutoConnection | Qt::UniqueConnection)));

    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }
}

/*
* Function:		_initUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormSettingLanguage::_initUi()
{
    qDebug() << Q_FUNC_INFO;

    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/setting/%2.qss").arg("skin_1").arg(this->objectName()));

    ui->pushButton_Chinese->setCheckable(true);
    ui->pushButton_Chinese->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_English->setCheckable(true);
    ui->pushButton_English->setFocusPolicy(Qt::NoFocus);
}

/*
 * Function:    on_pushButton_Chinese_clicked
 * Description: Choose Chinese as the default language
 * Paramer:     void
 * Return:      void
 */
void FormSettingLanguage::on_pushButton_Chinese_clicked()
{
    qDebug() << Q_FUNC_INFO;

    ui->pushButton_Chinese->setChecked(true);
    ui->pushButton_English->setChecked(false);

    _saveLanguageType();
}

/*
 * Function:    on_pushButton_English_clicked
 * Description: Choose English as the default language
 * Paramer:     void
 * Return:      void
 */
void FormSettingLanguage::on_pushButton_English_clicked()
{
    qDebug() << Q_FUNC_INFO;

    ui->pushButton_Chinese->setChecked(false);
    ui->pushButton_English->setChecked(true);

    // Save the current language
    _saveLanguageType();
}

/*
* Function:		slotRefreshUI
* Description:	The slot that refresh UI info
* Paramer:		void
* Return:		void
*/
void FormSettingLanguage::slotRefreshUI()
{
    qDebug() << Q_FUNC_INFO;

    // Get the language type
    ST_CFG_LANGUAGE stLangType;
    ConfigureBLL::getInstance(this)->loadLanguageType(stLangType);

    if(EUM_LANGUAGE_TYPE::E_LANG_ZH_CN == (EUM_LANGUAGE_TYPE)stLangType.eumLangType) {
        ui->pushButton_Chinese->setChecked(true);
        ui->pushButton_English->setChecked(false);
    }
    else {
        ui->pushButton_Chinese->setChecked(false);
        ui->pushButton_English->setChecked(true);
    }
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *event - the pointer of "QEvent"
* Return:		void
*/
void FormSettingLanguage::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
    {
        ui->retranslateUi(this);
        qDebug() << Q_FUNC_INFO;
        break;
    }
    default:
        break;
    }

    QWidget::changeEvent(e);
}

/*
 * Function:    _saveLanguageType
 * Description: Save the current language
 * Paramer:     void
 * Return:      void
 */
void FormSettingLanguage::_saveLanguageType()
{
    qDebug() << Q_FUNC_INFO;

    ST_CFG_LANGUAGE stLangType;

    stLangType.eumLangType = EUM_LANGUAGE_TYPE::E_LANG_ZH_CN;
    if(ui->pushButton_English->isChecked()) {
        stLangType.eumLangType = EUM_LANGUAGE_TYPE::E_LANG_EN_US;
    }

    // Set the language type
    ConfigureBLL::getInstance(this)->saveLanguageType(stLangType);

    // Emit an signal that switch the language on UI
    emit signalSwitchLanguage(stLangType.eumLangType);
}
